import flash.Error;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.external.ExternalInterface;
import flash.net.FileReference;
import flash.events.IEventDispatcher;

class UploadPipe extends flash.display.Sprite
{
	var index:Array<String>;
	
	private var browser:FileBrowser;
	
	static function main() 
	{
		var pipe:UploadPipe = new UploadPipe();
		flash.Lib.current.stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;//"noscale";
		flash.Lib.current.addChild(pipe);
        pipe.load();
	}
	
	public static function getParam()
	{
		return flash.Lib.current.loaderInfo.parameters;
	}
	
	public static function getId()
	{
		return getParam().id;
	}
	
	public function load()
	{
		var value:String = untyped flash.Lib.current.external;
		browser = new FileBrowser(true);
		browser.id = getId();
		
		index = new Array();
		index[0] = "mouseOver";
		index[1] = "mouseOut";
		
		flash.Lib.current.addEventListener(Event.ADDED_TO_STAGE,allLoaded);
		
		ExternalInterface.addCallback("remove", browser.remove);
		ExternalInterface.addCallback("upload", browser.upload);
		
		try {
			ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
			//ExternalInterface.call("Pipe.sendToJavaScript", "Everything is loaded up!");
		} catch(e:Error) {
			trace(e.message);
		}

	}

	function mouseOutLoaded(e:Event)
	{
		var dObj:DisplayObject = e.currentTarget.content;
		index[1] = dObj.name;
		addChild(dObj);
		blur(null);
	}
	
	function mouseOverLoaded(e:Event)
	{
		var dObj:DisplayObject = e.currentTarget.content;
		index[0] = dObj.name;
		addChild(dObj);
		blur(null);
	}
	
	function outProgress(event:ProgressEvent)
	{
		var percentLoaded:Float = event.bytesLoaded/event.bytesTotal;
		percentLoaded = Math.round (percentLoaded * 100);
		//trace("out Loading: " + percentLoaded + "%");
	}
	
	function overProgress(event:ProgressEvent)
	{
		var percentLoaded:Float = event.bytesLoaded/event.bytesTotal;
		percentLoaded = Math.round (percentLoaded * 100);
		//trace("over Loading: " + percentLoaded + "%");
	}
	
	function hover(e:Event) 
	{
		getChildByName(index[0]).alpha = 100;
		getChildByName(index[1]).alpha = 0;
	}
	
	function blur(e:Event)
	{
		getChildByName(index[0]).alpha = 0;
		getChildByName(index[1]).alpha = 100;
	}
	
	function allLoaded(e:Event)
	{
		
		var mouseOut:flash.net.URLRequest = new flash.net.URLRequest("http://localhost/upload-pipe/images/mouseOut.jpg");
		var mouseOver:flash.net.URLRequest = new flash.net.URLRequest("http://localhost/upload-pipe/images/mouseOver.jpg");

		var mouseOutLoader = new flash.display.Loader();
		var mouseOverLoader = new flash.display.Loader();
		
		mouseOutLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, mouseOutLoaded);
		mouseOutLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, outProgress);
		mouseOverLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, mouseOverLoaded);
		mouseOverLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, overProgress);

		mouseOverLoader.load(mouseOver);
		mouseOutLoader.load(mouseOut);
	
		buttonMode = true;
		addEventListener("click", browse);
		addEventListener(MouseEvent.MOUSE_OVER, hover);
		addEventListener(MouseEvent.MOUSE_OUT, blur);
	}
	
	function receivedFromJavaScript(value:String)
	{
    	trace(value);
    }
	
	function browse(e:MouseEvent) 
	{
		browser.browse();
		blur(null);
	} 
	
}
